package kr.co.tomatosystem.pdetools.shell.internal;

import java.io.File;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.handlers.HandlerUtil;

public abstract class AbstractShowInShellHandler extends AbstractHandler {

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        ISelection selection = HandlerUtil.getCurrentSelection(event);
        if (selection instanceof IStructuredSelection) {
            IStructuredSelection structuredSelection = (IStructuredSelection) selection;
            Object firstElement = structuredSelection.getFirstElement();
            IResource resource = (IResource) Platform.getAdapterManager().getAdapter(firstElement, IResource.class);
            if (resource == null) {
                return null;
            }

            IPath fullPath = ResourcesPlugin.getWorkspace().getRoot().getRawLocation().append(resource.getFullPath());
            doReveal(fullPath.toFile());
        }
        return null;
    }

    protected abstract void doReveal(File file);

}
